package com.xgit.iov.codegen.controller;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xgit.iov.codegen.entity.GenDatasource;
import com.xgit.iov.codegen.service.IGenDatasourceService;
import com.xgit.iov.common.core.util.R;
import com.xgit.iov.common.log.annotation.LogAnnotation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;

/**
 * 数据源管理
 *
 * @author william
 * @date 2020-02-16
 */

@AllArgsConstructor
@RestController
@RequestMapping("/datasource")
public class GenDatasourceController {

    private final DataSource dataSource;

    private final DataSourceCreator dataSourceCreator;

    @Autowired
    private IGenDatasourceService genDatasourceService;

    /**
     * 查询数据源列表
     */
    @GetMapping("/page")
    public R list(Page page, GenDatasource genDatasource) {
        return R.ok(genDatasourceService.selectList(page, genDatasource));
    }


    @GetMapping("/list")
    public R selectAllList() {
        return R.ok(genDatasourceService.selectAllList());
    }


    /**
     * 获取数据源详细信息
     */
    @GetMapping(value = "/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(genDatasourceService.getById(id));
    }

    /**
     * 新增数据源
     */
	@LogAnnotation("新增数据源")
    @PostMapping(value = "/save")
    public R save(@RequestBody GenDatasource genDatasource) {

        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        dataSourceProperty.setPollName(genDatasource.getPollName());
        dataSourceProperty.setUrl(genDatasource.getUrl());
        dataSourceProperty.setDriverClassName(genDatasource.getDriverClassName());
        dataSourceProperty.setUsername(genDatasource.getUsername());
        dataSourceProperty.setPassword(genDatasource.getPassword());

        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
        ds.addDataSource(genDatasource.getPollName(), dataSource);

        return R.ok(genDatasourceService.insertGenDatasource(genDatasource));
    }

    /**
     * 修改数据源
     */
	@LogAnnotation("修改数据源")
	@PutMapping(value = "/update")
    public R update(@RequestBody GenDatasource genDatasource) {
        return R.ok(genDatasourceService.updateGenDatasource(genDatasource));
    }

    /**
     * 删除数据源
     */
	@LogAnnotation("删除数据源")
	@DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids) {
        for (Long id : CollUtil.toList(ids)){
            GenDatasource datasource  = genDatasourceService.getById(id);
            DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
            ds.removeDataSource(datasource.getPollName());
        }
        return R.ok(genDatasourceService.deleteGenDatasourceByIds(ids));
    }

}
